package com.mhkj.base.earlyWarning.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.base.earlyWarning.dao.EarlyWarningDao;
import com.mhkj.base.earlyWarning.entity.EarlyWarning;
import com.mhkj.base.earlyWarning.service.EarlyWarningService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class EarlyWarningServiceImpl extends BaseServiceImpl<EarlyWarning> implements EarlyWarningService {
	@Resource
	private EarlyWarningDao EarlyWarningDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setEarlyWarningDao(EarlyWarningDao EarlyWarningDao) {
		super.setBaseRepository(EarlyWarningDao);
	}

	@Override
	public Pagination<EarlyWarning> listByPage(EarlyWarning EarlyWarning, int pageIndex, int size ) {
		Specification<EarlyWarning> spec = new Specification<EarlyWarning>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<EarlyWarning> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
 		 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<EarlyWarning> pageT = EarlyWarningDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<EarlyWarning> pagination = new Pagination<EarlyWarning>();
		pagination.setTotal(EarlyWarningDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public EarlyWarning findByStatus(Integer status) {
		// TODO Auto-generated method stub
		return EarlyWarningDao.findByStatus(status);
	}

}
